package com.ruoyi.erp.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.erp.domain.ProjectHour;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.ProjectHourAbnormal;
import com.ruoyi.erp.service.IProjectHourAbnormalService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 异常工时Controller
 * 
 * @author sauger
 * @date 2022-04-13
 */
@Controller
@RequestMapping("/erp/projectHourAbnormal")
public class ProjectHourAbnormalController extends BaseController
{
    private String prefix = "erp/projectHour/abnormal";

    @Autowired
    private IProjectHourAbnormalService projectHourAbnormalService;

    @RequiresPermissions("erp:projectHourAbnormal:view")
    @GetMapping()
    public String projectHourAbnormal(ModelMap mmap)
    {
        Date firstDay = DateUtils.getMondayDate();
        Date lastDay = DateUtils.addDays(firstDay,7);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        mmap.put("beginTime", format.format(firstDay));
        mmap.put("endTime", format.format(lastDay));
        return prefix + "/projectHourAbnormal";
    }

    /**
     * 查询异常工时列表
     */
    @RequiresPermissions("erp:projectHourAbnormal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectHourAbnormal projectHourAbnormal)
    {
        startPage();
        List<ProjectHourAbnormal> list = projectHourAbnormalService.selectProjectHourAbnormalList(projectHourAbnormal);
        return getDataTable(list);
    }

    /**
     * 导出异常工时列表
     */
    @RequiresPermissions("erp:projectHourAbnormal:export")
    @Log(title = "异常工时", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectHourAbnormal projectHourAbnormal)
    {
        List<ProjectHourAbnormal> list = projectHourAbnormalService.selectProjectHourAbnormalList(projectHourAbnormal);
        ExcelUtil<ProjectHourAbnormal> util = new ExcelUtil<ProjectHourAbnormal>(ProjectHourAbnormal.class);
        return util.exportExcel(list, "异常工时数据");
    }

    /**
     * 新增异常工时
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Date firstDay = DateUtils.getMondayDate();
        Date lastDay = DateUtils.addDays(firstDay,7);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        mmap.put("beginTime", format.format(firstDay));
        mmap.put("endTime", format.format(lastDay));
        return prefix + "/add";
    }

    /**
     * 新增保存异常工时
     */
    @RequiresPermissions("erp:projectHourAbnormal:add")
    @Log(title = "异常工时", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectHourAbnormal projectHourAbnormal)
    {
        return toAjax(projectHourAbnormalService.insertProjectHourAbnormal(projectHourAbnormal));
    }

    @RequiresPermissions("erp:projectHourAbnormal:add")
    @Log(title = "异常工时", businessType = BusinessType.INSERT)
    @PostMapping("/calculate")
    @ResponseBody
    public AjaxResult calculate(ProjectHourAbnormal projectHourAbnormal)
    {
        Date beginDate = DateUtils.parseDate(projectHourAbnormal.getParams().get("beginDate"));
        Date endDate = DateUtils.parseDate(projectHourAbnormal.getParams().get("endDate"));
        Integer holidayConfigType = Integer.parseInt(projectHourAbnormal.getParams().get("holidayConfig").toString());
        projectHourAbnormalService.calculateProjectHourAbnormal(holidayConfigType,beginDate, endDate);
        return toAjax(true);
    }

    /**
     * 修改异常工时
     */
    @RequiresPermissions("erp:projectHourAbnormal:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ProjectHourAbnormal projectHourAbnormal = projectHourAbnormalService.selectProjectHourAbnormalById(id);
        mmap.put("projectHourAbnormal", projectHourAbnormal);
        return prefix + "/edit";
    }

    /**
     * 修改保存异常工时
     */
    @RequiresPermissions("erp:projectHourAbnormal:edit")
    @Log(title = "异常工时", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectHourAbnormal projectHourAbnormal)
    {
        return toAjax(projectHourAbnormalService.updateProjectHourAbnormal(projectHourAbnormal));
    }

    /**
     * 删除异常工时
     */
    @RequiresPermissions("erp:projectHourAbnormal:remove")
    @Log(title = "异常工时", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(projectHourAbnormalService.deleteProjectHourAbnormalByIds(ids));
    }
}
